package com.zktr.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zktr.bean.BrandBean;
import com.zktr.bean.GoodTypeBean;
import com.zktr.pojo.PageBean;
import com.zktr.service.GoodTypeService;

/**
 * @author yf  
 * @version 创建时间：2018年9月7日  上午1:57:26
 * tags
 */
@WebServlet("/GoodTypeServlet")
public class GoodTypeServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	GoodTypeService gService =new GoodTypeService();
	public void quaryAllType(HttpServletRequest req,HttpServletResponse res) throws IOException {
		PrintWriter out = res.getWriter();
		int pc = getPc(req);

		int ps = 10;
		PageBean<GoodTypeBean> pageBean = gService.findAllGoodsType(pc, ps);
		pageBean.setUrl(getUrl(req));
		req.getSession().setAttribute("pageBean",pageBean.getBeanList() );
		req.getSession().setAttribute("pb",pageBean);
		out.println("0");
	}
	private int getPc(HttpServletRequest req) {
		String value = req.getParameter("pc");
		if(value == null || value.trim().isEmpty()) {
			return 1;
		}
		try{
			return Integer.parseInt(value);
		}catch(Exception e){
			return  1;
		}
	}
	private String getUrl(HttpServletRequest request) {
		String contextPath = request.getContextPath();
		String servletPath = request.getServletPath();
		String queryString = request.getQueryString();
		if(queryString.contains("&pc=")) {
			int index = queryString.lastIndexOf("&pc=");
			queryString = queryString.substring(0, index);
		}
		return contextPath + servletPath + "?" + queryString;
	}


}